package com.geeksonsecurity.malwaredemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.geeksonsecurity.malwaredemo.domain.OverlayType;
import com.geeksonsecurity.malwaredemo.domain.Settings;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setTitle(getTitle());
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setIcon(R.mipmap.ic_launcher);
        }

        startService();
    }

    @Override
    protected void onStart() {
        super.onStart();

        final Settings s = Settings.Load(getApplicationContext());
        final CheckBox skype = (CheckBox) findViewById(R.id.skypeCheckbox);
        final CheckBox linkedin = (CheckBox) findViewById(R.id.linkedinCheckbox);
        final CheckBox ubsBanking = (CheckBox) findViewById(R.id.ubsCheckbox);

        skype.setChecked(s.isSkypeEnabled());
        linkedin.setChecked(s.isLinkedinEnabled());
        ubsBanking.setChecked(s.isUbsEnabled());

        Button save = (Button) findViewById(R.id.saveButton);
        final Spinner overlayTypeSpinner = (Spinner) findViewById(R.id.overlayTypeSpinner);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OverlayType overlayType = (OverlayType) overlayTypeSpinner.getSelectedItem();
                s.setSkypeEnabled(skype.isChecked());
                s.setUbsEnabled(ubsBanking.isChecked());
                s.setLinkedinEnabled(linkedin.isChecked());
                s.setOverlayType(overlayType);
                Settings.Save(getApplicationContext(), s);
                Toast.makeText(MainActivity.this, "Saved!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), MainService.class);
                intent.setAction(ServiceCommunication.UPDATE_SETTINGS);
                startService(intent);
            }
        });

        ArrayAdapter<OverlayType> detectionEngineArrayAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, OverlayType.values());
        overlayTypeSpinner.setAdapter(detectionEngineArrayAdapter);
        overlayTypeSpinner.setSelection(detectionEngineArrayAdapter.getPosition(s.getOverlayType()));

        TextView footer = (TextView) findViewById(R.id.footer);
        footer.setText(Html.fromHtml(getString(R.string.footer)));
    }

    private void startService() {
        Intent intent = new Intent(this, MainService.class);
        startService(intent);
    }
}